#!/usr/bin/python3
import os
import time
import pandas as pd

# session_file_path = r'../Session.csv'
session_file_path = r'../Session1.csv'


def main():
    # deviceId,localeId,sessionId,sessionStartedAt,sessionEndedAt,province,city,area,community,building,unit,elevator,side,eventTime,eventDate,deviceConfigBrightness,deviceConfigVolume,adType,adId,adDisplayID,adDisplayStartedAt,adDisplayEndedAt,adDuration,adVersionNumber,riderId,riderClientFaceId,riderServerFaceId,riderEnteredAt,riderLeavedAt,riderDuration,riderVersionNumber,riderWatchTotalWatchDuration,riderWatchFirstStartedAt,riderWatchFirstEndedAt,riderWatchFirstDuration,riderWatchLastStartedAt,riderWatchLastEndedAt,riderWatchLastDuration,riderWatchWatches,riderTagGender,riderTagAge,riderTagEyeglasses,riderTagPets,riderTagStroller
    # session_cvs = pd.read_csv(session_file_path, sep=';')
    session_cvs = pd.read_csv(session_file_path, sep=',')

    deviceIds = session_cvs['deviceId']
    localeIds = session_cvs['localeId']
    sessionIds = session_cvs['sessionId']
    sessionStartedAts = session_cvs['sessionStartedAt']
    sessionEndedAts = session_cvs['sessionEndedAt']
    eventTimes = session_cvs['eventTime']
    eventDates = session_cvs['eventDate']
    riderIds = session_cvs['riderId']
    riderEnteredAts = session_cvs['riderEnteredAt']
    riderLeavedAts = session_cvs['riderLeavedAt']
    riderDurations = session_cvs['riderDuration']

    device_locale_id = {}
    session_infos = {}
    device_session_rider_infos = {}
    for deviceId, localeId, sessionId, sessionStartedAt, sessionEndedAt, eventTime, eventDate, riderId, riderEnteredAt, riderLeavedAt, riderDuration in zip(deviceIds, localeIds, sessionIds, sessionStartedAts, sessionEndedAts, eventTimes, eventDates, riderIds, riderEnteredAts, riderLeavedAts, riderDurations):
        device_locale_id[deviceId] = localeId
        sessionStartedAt = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(sessionStartedAt) // 1000 + 3600 * 8))
        sessionEndedAt = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(sessionEndedAt) // 1000 + 3600 * 8))
        riderEnteredAt = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(riderEnteredAt) // 1000 + 3600 * 8))
        riderLeavedAt = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(riderLeavedAt) // 1000 + 3600 * 8))
        session_infos[sessionId] = [sessionStartedAt, sessionEndedAt]
        riderDuration //= 1000

        session_rider_infos = device_session_rider_infos.get(deviceId, None)
        if session_rider_infos is None:
            rider_infos = {riderId: [riderEnteredAt, riderLeavedAt, riderDuration]}
            device_session_rider_infos[deviceId] = {sessionId: rider_infos}
        else:
            rider_infos = session_rider_infos.get(sessionId, None)
            if rider_infos is None:
                rider_infos = {riderId: [riderEnteredAt, riderLeavedAt, riderDuration]}
                session_rider_infos[sessionId] = rider_infos
            else:
                rider_infos[riderId] = [riderEnteredAt, riderLeavedAt, riderDuration]

    for device_id, locale_id in device_locale_id.items():
        print('Device: {}, Locale: {}'.format(device_id, locale_id))
        session_rider_infos = device_session_rider_infos[device_id]
        for session_id, rider_infos in session_rider_infos.items():
            sessionStartedAt, sessionEndedAt = session_infos[session_id]
            print('  Session: {}, {} - {}'.format(session_id, sessionStartedAt, sessionEndedAt))
            rider_info_list = []
            for ride_id, rider_info in rider_infos.items():
                rider_info_list.append([ride_id, *rider_info])
            rider_info_list.sort(key=lambda x: x[-1], reverse=True)
            for rider_info in rider_info_list:
                print('    Rider: {}, {} - {}, {}s'.format(*rider_info))


if __name__ == '__main__':
    main()
